home *** CD-ROM | disk | FTP | other *** search
- Path: halon.vggas.com!news
- From: JYoungman@vggas.com (James Youngman)
- Newsgroups: comp.lang.c++
- Subject: Re: Array Summary Algorithm
- Date: 12 Apr 1996 11:10:17 GMT
- Organization: VG Gas Analysis Systems
- Message-ID: <4kldmp$5m8@halon.vggas.com>
- References: <4kej9q$fhf$1@mhadg.production.compuserve.com>
- NNTP-Posting-Host: 132.147.163.4
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=US-ASCII
- X-Newsreader: WinVN 0.99.7
-
- In article <4kej9q$fhf$1@mhadg.production.compuserve.com>,
- 75226.1623@CompuServe.COM says...
- >
- >Has anyone ever heard of, or seen an algorithm that can summarize
- >the contents of an array. What I mean is this... if an array
- >looks like this:
- >
- >New York 10
- >Delaware 12
- >New York 15
- >Indiana 12
- >Delaware 10
- >
- >Create a summary array from the preceeding to look like this:
- >New York 25
- >Delaware 22
- >Indiana 12
- >
-
- This looks to me like an aplication for a sort followed by
- some trivial code. You could use qsort() for the sort, if
- you wanted. Also, I seem to remember that Jon Bentley covered
- this in "More Programming Pearls", ISBN 0-201-11889-0 (this is
- a book I strongly reccommend, along with its predecessor,
- "Programming Pearls", ISBN 0-201-10331-1.
-
- Here's his code for this (page 17):-
-
- #!/usr/bin/awk
- { count[$1] = count[$1] + $2 }
- END { for (i in count) print i, count[i] }
-
- Something very similar is just as trivial in Perl.
-
- James Youngman
- VG Gas Analysis Systems
-
-
-
-